package com.metagurukul.informer.servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import com.metagurukul.informer.service.*;

/**
 * Servlet implementation class AskQuestion
 */
@WebServlet("/AskQuestion")
public class AskQuestion extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
/**
 * servlet for ask questions
 *  @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session=request.getSession();
			String title=request.getParameter("title");
			String description=request.getParameter("description");
			String tags=request.getParameter("tags");
			QuestionsService service=new QuestionsService();
			String user=null;
			if(session.getAttribute("userName")!=null){
				user=(String)session.getAttribute("userName");
			}
			else{
				user=(String)session.getAttribute("adminName");
			}
			int question_id=service.insert(title,description,tags,user);
			response.sendRedirect("ShowQuestion?id="+question_id);
		} catch (SQLException e) {
			response.sendRedirect("Error.jsp");
		}
	}

}
